package com.gitee.form_token.entity;

import com.gitee.form_token.dto.MenuDto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyUserDetails implements UserDetails, Serializable {

    private SysAccount account;

    private List<MenuDto> menuTree;

    private List<String> authorityList;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities = authorityList.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());

        // authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        // authorities.add(new SimpleGrantedAuthority("ROLE_USER"));

        return authorities;
    }

    @Override
    public String getUsername() {
        return account.getUsername();
    }

    @Override
    public String getPassword() {
        return account.getPassword();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;// 表示账号没有过期
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;// 表示账号没有被锁定
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;// 表示密码没有过期
    }

    @Override
    public boolean isEnabled() {
        return true;// 表示账号可用
    }
}
